package main

import (
	"fmt"
	"github.com/garyburd/redigo/redis"
	"strconv"
	"testproject/source/util/hmap"
	"testproject/source/util/list"
)

func main() {
	c, err := redis.Dial("tcp", "localhost:6379")
	if err != nil {
		fmt.Println("Connect to redis error", err)
		return
	}
	defer c.Close()
	for i := 10; i < 20; i++ {
		strs := make([]interface{}, 7)
		strs[0] = "key" + strconv.Itoa(i)
		strs[1] = "uid"
		strs[2] = "1002" + strconv.Itoa(i)
		strs[3] = "uname"
		strs[4] = "wangwu" + strconv.Itoa(i)
		strs[3] = "uage"
		strs[4] = "1" + strconv.Itoa(i)
		c.Do("hmset", strs...)
	}
	lst := list.New()
	for i := 10; i < 15; i++ {
		hmp := Gets(c, "hmget", "key"+strconv.Itoa(i), "uid", "uname", "uage")
		lst.Add(hmp)
	}
	hmp := lst.Get(4).(hmap.HashMap)
	fmt.Println(lst, hmp.Get("uid"), hmp.Get("uname"), hmp.Get("uage"))

	tt := Test{1}
	tt.Set(10)
	TifaceGet(&tt)
	TifaceSet(&tt, 11)
	fmt.Println(tt.id)
	c.Do("save")

}
func Gets(rc redis.Conn, commandName string, args ...interface{}) hmap.HashMap {
	str, err := rc.Do(commandName, args...)
	hmp := hmap.New()
	if err != nil {
		fmt.Println("error:", err)
	} else {
		obj := str.([]interface{})
		size := len(obj)
		for i := 0; i < size; i++ {
			value, err := obj[i].([]byte)
			if err {
				hmp.Set(args[i+1], string(value))
			}
		}
	}
	return hmp
}

type Test struct {
	id int
}
type TestInnterface interface {
	Get() int
	Set(id int)
}

func TifaceGet(ti TestInnterface) {
	fmt.Println(ti.Get())
}
func TifaceSet(ti TestInnterface, id int) {
	ti.Set(id)
}
func (t *Test) Get() int {
	return t.id
}
func (t *Test) Set(id int) {
	t.id = id
}
